package com.jh3502.helper.function.person.personnew;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.jh3502.helper.R;
import com.jh3502.helper.utils.listener.MyFocusChangeListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author harisucici
 */
public class PersonNewAdapter extends RecyclerView.Adapter<PersonNewAdapter.ViewHolder> {


    private int add;// recycler控件不是0开始的
    private List<PersonNewBean> data;
    private Context mContext;
    private List<String> list;
    private View convertView;
    private ButtonInterface viewClick;

    public PersonNewAdapter(Context context, List<PersonNewBean> data, List<String> list, int add) {
        this.mContext = context;
        this.data = data;
        this.list = list;
        this.add = add;

    }

    public void setButtonSetOnclick(ButtonInterface buttonInterface) {
        this.viewClick = buttonInterface;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_person_new, viewGroup, false);
        ViewHolder vh = new ViewHolder(convertView);
        //将创建的View注册点击事件
        return vh;

    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int position) {

        final PersonNewBean label = data.get(position);
        viewHolder.itemPersonNewTitle.setText(label.getName());
        viewHolder.itemPersonNewContent.setHint("请输入" + label.getName());
        viewHolder.itemPersonNewContent.setOnFocusChangeListener(
                new MyFocusChangeListener(mContext, label.getName(), position + add));
        if (list.size() > 0) {
            viewHolder.itemPersonNewContent.setText(list.get(position));
        }
        viewHolder.itemPersonNewContent.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (viewClick != null) {
                    //接口实例化后的而对象，调用重写后的方法
                    viewClick.onClick(position);
                }
                return false;
            }
        });

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public interface ButtonInterface {
        void onClick(int position);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_person_new_title)
        TextView itemPersonNewTitle;
        @BindView(R.id.item_person_new_content)
        EditText itemPersonNewContent;

        public ViewHolder(@NonNull final View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}

